import QtQuick
import QtQuick.Controls
import QtQuick.Layouts

Window {
    visible: true
    width: 400
    height: 600
    title: "SwipeDelegate Demo"

    ListModel {
        id: listModel
        ListElement { name: "Item 1" }
        ListElement { name: "Item 2" }
        ListElement { name: "Item 3" }
    }

    ColumnLayout {
        anchors.fill: parent

        // 通过外部按钮控制滑动

        RowLayout {
            Layout.fillWidth: true
            height: 30

            Button {
                text: "Show Options"
                onClicked: {
                    // 打开第一个 SwipeDelegate 的右侧区域
                    if (listView.itemAtIndex(0)) {
                        listView.itemAtIndex(0).swipe.open(SwipeDelegate.Right);
                    }
                }
            }

            Button {
                text: "Hide Options"
                onClicked: {
                    // 关闭第一个 SwipeDelegate 的右侧区域
                    if (listView.itemAtIndex(0)) {
                        listView.itemAtIndex(0).swipe.close(SwipeDelegate.Right);
                    }
                }
            }
        }

        ListView {
            id: listView
            Layout.fillWidth: true
            Layout.fillHeight: true
            model: listModel
            delegate: SwipeDelegate {
                id: swipeDelegate
                text: name
                width: listView.width

                // 在自定义组件中响应点击
                swipe.right: Rectangle {
                    width: parent.width
                    height: parent.height
                    color: "#f0f0f0"

                    Label {
                        text: "Click"
                        color: "#333"
                        anchors.centerIn: parent
                    }

                    MouseArea {
                        anchors.fill: parent
                        onClicked: console.log("clicked : " + name)
                    }
                }
            }
        }
    }
}
